package com.ims.repository;

import com.ims.entity.MediaAccountEntity;
import com.ims.entity.vo.MediaListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class MediaAccountRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<MediaAccountEntity> getList(Integer isLive){
        String sql = "SELECT * FROM media_account WHERE 1=1 ";
        List<Object> params = new ArrayList<>();


        if (isLive!=null ) {
            sql += " AND isLive =? ";
            params.add(isLive);
        }


        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(MediaAccountEntity.class),params.toArray(new Object[params.size()]));
    }

    public List<MediaListVo> getMediaList(Integer isLive){
        String sql = "SELECT id as `value`,concat_ws(' - ',`type`,`name`) as text FROM media_account WHERE 1=1 ";
        List<Object> params = new ArrayList<>();


        if (isLive!=null ) {
            sql += " AND isLive =? ";
            params.add(isLive);
        }
        sql+= " ORDER BY `type` asc,id desc ";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(MediaListVo.class),params.toArray(new Object[params.size()]));
    }
}
